#pragma oncel
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <algorithm>
#include "/home/hu/http-server-source-code/HTTP/Log.hpp"
bool GetQUeryString(std::string& query_string)
{
    bool result =false;
    std::string method=getenv("METHOD");
    // std::cerr<<method<<std::endl;
    if(method == "GET")
    {
        query_string=getenv("QUERY_STRING");
        result=true;
    }
    else if(method == "POST")
    {   
        // std::cerr<<"diwapdispad"<<std::endl;
        int content_length=atoi(getenv("CONTENT_LENGTH"));
        // std::cerr<<content_length<<std::endl;
        
        char ch='0';
        while(content_length > 0)
        {
            // std::cerr<<ch<<std::endl;
            int x=0;
            if(x=read(0,&ch,1))
            {
                query_string.push_back(ch);
                content_length--;
            }
            // std::cerr<<x<<std::endl;
            
        }
        // std::cerr<<query_string.size()<<std::endl;
        result=true;
    }
    else
    {
        LOG(WARENING,"method error");
        result=false;
    }
    return result;
}

bool CutString(std::string str,std::string& out1,std::string& out2,std::string sep)
{
    size_t pos=str.find(sep);
    if(pos != std::string::npos)
    {
        out1=str.substr(0,pos);
        out2=str.substr(pos+1);
        return true;
    }
    else
    {
        
        LOG(ERROR,"query_string sub failed");
    }
    return false;
}



